﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ProductManagement;

public partial class Modules_Purchase_AddMaterials : System.Web.UI.UserControl
{
    Products _products = new Products();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["Purchase_SearchMaterials"] = string.Empty;
            Search();           
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["Purchase_SearchMaterials"] = txtSearch.Text;
        Search();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Session["Purchase_Add/Edit"] = "Add";
        tblSearch.Visible = false;
        tblAddMaterials.Visible = true;
        lblMaterialTitle.Text = "ADD MATERIALS";
        btnSave.Text = "Save";
        FillDDL();
    }

   

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        //if (CheckSelection())
        //{
            CheckSelection();
            Session["Purchase_Add/Edit"]="Edit";
            tblSearch.Visible = false;
            tblAddMaterials.Visible = true;
            lblMaterialTitle.Text = "EDIT MATERIALS";
            btnSave.Text = "Update";
            LoadData();
            FillDDL();
        //}

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Delete();
        Search();
    }

    protected void btnAddCategory_Click(object sender, EventArgs e)
    {

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        tblSearch.Visible = true;
        tblAddMaterials.Visible = false;
        Clear();
        Search();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["Purchase_Add/Edit"].ToString() == "Add")
            Save();
        else if (Session["Purchase_Add/Edit"].ToString() == "Edit")
        {
            Edit();
            Clear();
            tblSearch.Visible = true;
            tblAddMaterials.Visible = false;
            Search();
        }
    }

    void Search()
    {
        DataSet dsMaterials = new DataSet();
//        _products.SearchMaterials(Session["Purchase_SearchMaterials"].ToString(), out dsMaterials);
        gvwMaterials.DataSource = dsMaterials;
        gvwMaterials.Columns[1].Visible = true;
        gvwMaterials.DataBind();
        gvwMaterials.Columns[1].Visible = false;

    }

    void FillDDL()
    {
        try
        {
            //fill category
            DataSet dsVendorCategory = _products.GetData("MaterialCategory", "");
            ddlCategory.DataSource = dsVendorCategory;
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataBind();
            ddlCategory.Items.Insert(0, "Select");

            //fill vendor
            dsVendorCategory.Clear();
            dsVendorCategory = _products.GetData("VendorDetails", "");
            ddlVendor.DataSource = dsVendorCategory;
            ddlVendor.DataTextField = "VendorName";
            ddlVendor.DataValueField = "VendorID";
            ddlVendor.DataBind();
            ddlVendor.Items.Insert(0, "Select");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Fill DropDownList Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

    void Save()
    {
        try
        {
            Materials material = new Materials();
            material.MaterialName = txtMaterialName.Text;
            material.CategoryID = ddlCategory.SelectedItem.Value;
            material.VendorID = ddlVendor.SelectedItem.Value;
            material.Price = txtPrice.Text;

            //if (_products.AddMaterials(material))
            //{
            //    Response.Write("<Script>alert('Added Successfully !')</Script>");
            //    Clear();
            //}
            //else
            //    Response.Write("<Script>alert('Already Exist !')</Script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add Materials Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Clear()
    {
        txtMaterialName.Text = string.Empty;
        ddlCategory.ClearSelection();
        ddlVendor.ClearSelection();
        txtPrice.Text = string.Empty;
    }

    /// <summary>
    /// check which rows of the grid view are selected
    /// </summary>
    /// <returns></returns>
    void CheckSelection()
    {
        try
        {
            int count = 0;
            int index = 0;            

            for (int i = 0; i < gvwMaterials.Rows.Count; i++)
            {
                CheckBox chkGrid = (CheckBox)(gvwMaterials.Rows[i].Cells[0].FindControl("chkEditDelete"));
                if (chkGrid.Checked == true)
                {
                    index = i;                  
                    count++;
                }
            }
           
            Session["Purchase_MaterialID"] = gvwMaterials.Rows[index].Cells[1].Text;
         
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Check Selection Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
      
    }

    void LoadData()
    {
        string condition = "MaterialID = '" + Session["Purchase_MaterialID"].ToString() + "'";
        DataSet dsMaterialsEdit = _products.GetData("Materials", condition);
        txtMaterialName.Text = dsMaterialsEdit.Tables[0].Rows[0]["MaterialName"].ToString();
        txtPrice.Text = dsMaterialsEdit.Tables[0].Rows[0]["Price"].ToString();
    }

    void Edit()
    {
        Materials material = new Materials();
        material.MaterialID = Session["Purchase_MaterialID"].ToString();
        material.MaterialName = txtMaterialName.Text;
        material.CategoryID = ddlCategory.SelectedItem.Value;
        material.VendorID = ddlVendor.SelectedItem.Value;
        material.Price = txtPrice.Text;

//        _products.EditMaterials(material);
    }

    void Delete()
    {

        try
        {         
            int index = 0;
            string[] materialID = new string[gvwMaterials.Rows.Count];           
            for (int i = 0; i < gvwMaterials.Rows.Count; i++)
            {
                CheckBox chkGrid = (CheckBox)(gvwMaterials.Rows[i].Cells[0].FindControl("chkEditDelete"));
                if (chkGrid.Checked == true)
                {
                    materialID [index] = gvwMaterials.Rows[i].Cells[1].Text;
                    index++;
                }
            }         

            string conditionDelete = "MaterialID='" + materialID[0] + "'";          
           
            for (int i = 1; i < index; i++)
            {
                conditionDelete = conditionDelete + "or MaterialID='" + materialID[i] + "'";                
            }

//            _products.DeleteMaterials(conditionDelete);
            Response.Write("<script>alert('Deleted Successfully !')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Delete Material Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }


    protected void gvwMaterials_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";

        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }
}



   

